//
//  ModelBase.m
//  DataCenter
//
//  Created by Tang Haibo on 12-6-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "QBModelBase.h"
@interface QBModelBase()

@property (nonatomic, retain) NSDictionary *dict;

- (void)setupObjAttributeWithDict:(NSDictionary *)dict;

@end

@implementation QBModelBase


#pragma mark - public
- (void)dealloc{
    [_dict release];
    [super dealloc];
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
//    [super encodeWithCoder:aCoder];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if(self)
    {
        
    }
    return self;
}

- (id)initWithDict:(NSDictionary *)dict{
    self = [super init];
    if (nil!=self) {
        self.dict = dict;
        [self setupObjAttributeWithDict:dict];
    }
    return self;
}

- (BOOL)isParseSucceed{
    //note: 如果dict中的数据要是该对象的子集则验证成功。换句话说，如果dict的数据没有完全映射到该对象中，则验证失败。
    NSDictionary *attrMapDict = [self attributedMapDictionary];
    NSEnumerator *keyEnum = [self.dict keyEnumerator];
    NSString *jsonKey = nil;
    while (nil!=(jsonKey = [keyEnum nextObject])) {
        //jsonKey->NSDictionary/NSString
        id keyForAttr = [attrMapDict objectForKey:jsonKey];
        if ([keyForAttr isKindOfClass:[NSDictionary class]]) {
            keyForAttr = KEY_FROM_ATTRIBUTE_INFO(keyForAttr);
        }
        
        id attr = [self valueForKey:keyForAttr];
        //数组
        if ([attr isKindOfClass:[NSArray class]]) {
            for (id obj in attr) {
                if ([obj isKindOfClass:[QBModelBase class]]) {
                    if (![obj isParseSucceed]) {
                        return NO;
                    }
                }else if (![attr isEqual:[self.dict objectForKey:jsonKey]]) {
                    return NO;
                }
            }
        }else
        //字典
        if ([attr isKindOfClass:[NSDictionary class]]) {
            NSEnumerator *attrKeyEnum = [self.dict keyEnumerator];
            NSString *attrJsonKey = nil;
            while (nil!=(attrJsonKey = [attrKeyEnum nextObject])) {
                id obj = [attr objectForKey:attrJsonKey];
                if ([obj isKindOfClass:[QBModelBase class]]) {
                    if (![obj isParseSucceed]) {
                        return NO;
                    }
                }else if (![attr isEqual:[self.dict objectForKey:jsonKey]]) {
                    return NO;
                }
            }
        }else
        //其他
        if ([attr isKindOfClass:[QBModelBase class]]) {
            if (![attr isParseSucceed]) {
                return NO;
            }
        }else if (![attr isEqual:[self.dict objectForKey:jsonKey]]) {
            return NO;
        }
    }
    return YES;
}

#pragma mark - private
- (void)setupObjAttributeWithDict:(NSDictionary *)dict{
    if (dict==nil || ![dict isKindOfClass:[NSDictionary class]]) {
        return;
    }
    
    NSDictionary *attrMapDic = [self attributedMapDictionary];
    if (nil==attrMapDic || [attrMapDic count] == 0) {
        return;
    }
    
    NSEnumerator *keyEnum = [attrMapDic keyEnumerator];
    id jsonKey;
    while ((jsonKey = [keyEnum nextObject])) {
//        NSLog(@"%@", jsonKey);
        id value = [dict objectForKey:jsonKey];
        id attributeContainer = [attrMapDic objectForKey:jsonKey];
        NSString *attributeName = nil;
        if ([attributeContainer isKindOfClass:[NSString class] ] ) {
            attributeName = attributeContainer;
            
        }else{
            attributeName = KEY_FROM_ATTRIBUTE_INFO(attributeContainer);
            NSString *classNameForSubobj = KEYCLASSNAME_FROM_ATTRIBUTE_INFO(attributeContainer);
            if ([value isKindOfClass:[NSDictionary class] ]) {
                Class class = NSClassFromString(classNameForSubobj);
                if ([class isSubclassOfClass:[QBModelBase class] ]) {
                    value = [[[class alloc] initWithDict:value] autorelease];
                }
            }else if ([value isKindOfClass:[NSArray class] ]){
                Class class = NSClassFromString(classNameForSubobj);
                if ([class isSubclassOfClass:[QBModelBase class] ]) {
                    NSMutableArray *rstArray = [NSMutableArray arrayWithCapacity:0];
                    for (NSDictionary *dict in value) {
                        QBModelBase *aModel = [[class alloc] initWithDict:dict];
                        [rstArray addObject:aModel];
                        [aModel release];
                    }
                    value = rstArray;
                }
            }
        }
        
        if (nil!=value) {
            if ([value isKindOfClass:[NSString class]] &&
                [value isEqual:@""]) {
                value = nil;
            }
            [self setValue:value forKey:attributeName];
        }
    }
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}
- (void)setNilValueForKey:(NSString *)key{}
#pragma mark - override
- (NSDictionary *)attributedMapDictionary{
    /*
     1、子类实现实现该方法:
     dict...
     [dict addEntriesFromDictionary:[super attributedMapDictionary]];
     
     2、键值说明
     value：对象属性名
     key：json键值
     
     带自定义类型的键值说明
     ATTRIBUTE_INFO(@"intrestedApps", @"MAppInfo"), @"intrested_app"
     value：intrestedApps MAppInfo类型
     key：intrested_app
     */
    return nil;
}
@end
